﻿using UnityEngine;
using System.Collections;

public class ModAttr : IModBase
{
    private int curHp = 0;

    private int maxHp = 0;

    private float speed = 2.0f;

    private bool dead = false;

    public const int GROUP_RED = 1;
    public const int GROUP_BLUE = 2;

    /// <summary>
    /// 阵营
    /// </summary>
    private int group = 0;

    public ModAttr(OwnerObj ownerObj) : base(ownerObj)
    {

    }

    public bool IsDead()
    {
        return dead;
    }

    public float GetSpeed()
    {
        return speed;
    }

    public void SetSpeed(float speed)
    {
        this.speed = speed;
    }

    public int GetCurHp()
    {
        return curHp;
    }

    public void SetCurHp(int curHp)
    {
        this.curHp = curHp;
    }

    public int LossHp(int lossHp, IUnitObject attackUnitObj)
    {
        if (lossHp <= curHp)
        {
            curHp -= lossHp;
        }
        else
        {
            lossHp = curHp;
            curHp = 0;
        }

        if (ownerObj.Is<IUnitObject>())
        {
            IUnitObject unitObj = ownerObj.Get<IUnitObject>();
            AllEvent.eventLossHp.Fire(unitObj, lossHp, attackUnitObj);
        }

        if (curHp <= 0)
        {
            if (!dead)
            {
                dead = true;

                GetModule().Get<ModAction>().Die();

                if (ownerObj.Is<IUnitObject>())
                {
                    AllEvent.eventDead.Fire(ownerObj.Get<IUnitObject>(), attackUnitObj);
                }
            }
        }

        return curHp;
    }
    
    public int GetMaxHp()
    {
        return maxHp;
    }

    public void SetMapHp(int maxHp)
    {
        this.maxHp = maxHp;
    }

    public int GetGroup()
    {
        return group;
    }

    public void SetGroup(int group)
    {
        this.group = group;
    }

    public float GetHpPercentValue()
    {
        if (maxHp > 0)
        {
            return curHp / (float)maxHp;
        }
        else
        {
            return 0;
        }
    }
}
